//
//  SEGBillSearchSubViewCell.m
//  SEGUhomePropertyBaseService
//
//  Created by Clavis on 2019/2/28.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGBillSearchSubViewCell.h"

@interface SEGBillSearchSubViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *feeLabel;
@property (nonatomic, strong) UILabel *leftFeeLabel;
@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UIImageView *lineView;

@end

@implementation SEGBillSearchSubViewCell

- (instancetype)initWithModel:(SEGOwesMonthDetailModel *)model {
    self = [super init];
    if (self) {
        self.titleLabel = ({
            UILabel *lable = [[UILabel alloc] init];
            [self addSubview:lable];
            lable.textColor = kColor_1;
            lable.font = kFont_13;
            lable.text = model.feeItemTypeName;
            lable.numberOfLines = 0;
            lable;
        });
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(18);
            make.bottom.mas_equalTo(-18);
            make.width.mas_lessThanOrEqualTo((LYScreenWidth() - 105) * 0.6);
            make.height.mas_greaterThanOrEqualTo(15);
        }];
        
        self.statusLabel = ({
            UILabel *lable = [[UILabel alloc] init];
            [self addSubview:lable];
            lable.font = kFont_11;
            lable.text = model.billPayState == SEGBillDetailDoLogicModelFeeStatus_PayAll ? @"已缴清" : @"未缴清";
            lable.textColor = model.billPayState == SEGBillDetailDoLogicModelFeeStatus_PayAll ? kColor_3 : kColor_S1;
            lable;
        });
        [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.centerY.equalTo(self.titleLabel);
        }];
        
        self.feeLabel = ({
            UILabel *lable = [[UILabel alloc] init];
            [self addSubview:lable];
            lable.textColor = kColor_1;
            lable.font = kFont_13;
            CGFloat fee = (fabs(model.fee.doubleValue) + fabs(model.lfree.doubleValue)) * MoneyRateChange;
            lable.text = [NSString stringWithFormat:@"%.2f", fee];
            lable;
        });
        [self.feeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.statusLabel.mas_left).offset(-10);
            make.centerY.equalTo(self.titleLabel);
            make.width.mas_lessThanOrEqualTo((LYScreenWidth() - 105) * 0.4);
        }];
        
        if (model.lfree.doubleValue != 0) {
            self.leftFeeLabel = ({
                UILabel *lable = [[UILabel alloc] init];
                [self addSubview:lable];
                lable.textColor = kColor_3;
                lable.font = kFont_11;
                lable.text = [NSString stringWithFormat:@"含违约金%.2f", [model.lfree doubleValue]*MoneyRateChange];
                lable;
            });
            
            [self.feeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.statusLabel.mas_left).offset(-10);
                make.centerY.equalTo(self.titleLabel).offset(-8);
                make.width.mas_lessThanOrEqualTo((LYScreenWidth() - 105) * 0.4);
            }];
            
            [self.leftFeeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.statusLabel.mas_left).offset(-10);
                make.centerY.equalTo(self.titleLabel).offset(8);
                make.width.mas_lessThanOrEqualTo((LYScreenWidth() - 105) * 0.4);
            }];
        }
        
        // 画虚线
        self.lineView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 49, LYScreenWidth() - 60, 1)];
        [self addSubview:self.lineView];
        UIGraphicsBeginImageContext(self.lineView.frame.size); //参数size为新创建的位图上下文的大小
        [self.lineView.image drawInRect:CGRectMake(0, 0, LYScreenWidth() - 60, 1)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapSquare); //设置线段收尾样式
        CGFloat length[] = {3,1.5}; // 线的宽度，间隔宽度
        CGContextRef line = UIGraphicsGetCurrentContext(); //设置上下文
        CGContextSetStrokeColorWithColor(line, kColor_L2.CGColor);
        CGContextSetLineWidth(line, 1); //设置线粗细
        CGContextSetLineDash(line, 0, length, 2);//画虚线
        CGContextMoveToPoint(line, 0, 0.0); //开始画线
        CGContextAddLineToPoint(line, LYScreenWidth() - 60, 0.0);//画直线
        CGContextStrokePath(line); //指定矩形线
        self.lineView.image = UIGraphicsGetImageFromCurrentImageContext();
        
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo(1);
        }];
        
//        self.layer.borderWidth = 1;
//        self.layer.borderColor = UIColor.redColor.CGColor;
    }
    return self;
}

- (void)hideLine {
    self.lineView.hidden = YES;
}

@end
